const api = require("../api");
//引入express模块
const express = require("express");
//引入连接池模块
const pool = require("../pool.js");
const jwt=require("jsonwebtoken")

//创建路由器对象
const loginRouter = express.Router();

//GET /login 地址:http://localhost:3333/loginhou

loginRouter.post("/", (req, res, next) => {
  let {admin_name,admin_pwd}=req.body
  let sql ="select *from hbb_admin where admin_name=?"
  pool.query(sql,[admin_name,admin_pwd],(err,result)=>{
    if(err){
      next(err)
      return
    }
    // console.log('数据库result',result);
    // console.log('请求req.body',req.body);
   
    
    if(result.length==0){
      res.send({"code":501,"msg":'您不是管理员，请联系管理员'})
    }else if(result[0].admin_pwd!==admin_pwd){
      res.send({"code":502,"msg":'密码错误，请重新输入密码'})
    }else{
      let token = jwt.sign({ admin_name: admin_name}, api.secret, {
        expiresIn: "2400h",
      })
      res.send({
        code: 200,
        message:'登录成功!',
        data:result,
        token: { token }
      })

    }

    

  } )
  

});
//暴露路由器对象
module.exports = loginRouter;
